﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Wizard.Data
{
    public abstract class CacheableObject : IDisposable
    {
        internal ObjectState mObjectState;
        internal string mDataStoreId;

        public void LoadFromDataStore(IDataStore dataStore)
        {
            var ps = FieldSerializer.Get(this);
            //using (var dataStoreObject = dataStore.GetRecordReader(this.GetType(), this.mDataStoreId))
            //{
            //    ps.LoadFromDataStore(dataStoreObject, this);
            //}
        }

        public void SaveToDataStore(IDataStore dataStore)
        {
            var ps = FieldSerializer.Get(this);
            //using (var dataStoreObject = dataStore.GetRecordWriter(this.GetType(), ref this.mDataStoreId))
            //{
            //    ps.SaveToDataStore(dataStoreObject, this);
            //}
        }
        
        #region IDisposable 

        protected virtual void Dispose(bool disposing)
        {
            // allow descendants override this
        }

        void IDisposable.Dispose()
        {
            if (!IsDisposed)
            {
                mObjectState |= ObjectState.Disposed;
                
                Dispose(true);
                GC.SuppressFinalize(this);
            }
        }

        ~CacheableObject()
        {
            Dispose(false);
        }

        public bool IsDisposed
        {
            get { return (mObjectState & ObjectState.Disposed) != 0; }
        }
        
        #endregion

        public CacheableObject()
        {

        }

        public bool IsModified
        {
            get
            {
                return (mObjectState & ObjectState.Modified) != 0;
            }
        }

        public string DataStoreId
        {
            get { return mDataStoreId; }
            set { mDataStoreId = value; }
        }
    }
}
